/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   DecisionStandardServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz.scientificDecision    </p>
 * 
 * <p>最后修改 : $: 2017年8月8日-下午7:33:10 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.scientificDecision;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jsbz.scientificDecision.DecisionStandardDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.DataMengement;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.scientificDecision.DecisionStandardService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * DecisionStandardServiceImpl
 * 
 * 2017年8月8日 下午7:33:10
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class DecisionStandardServiceImpl implements DecisionStandardService {
	
	@Autowired
	private DecisionStandardDao decisionStandardDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Override
	public List<DataMengement> getDataMengementList(QueryFilter queryFilter){
		return decisionStandardDao.find(queryFilter,"attmentYear", true);
	}
	
	@Override
	public Map<String, Object> getDataMengement(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<DataMengement> dataMengementList = new ArrayList<DataMengement>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取公路养护科学类文件
		List<Attachment> yhkxList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "jcgf_yhkx");
		//获取年度养护文件
		//获取年度文件
		//养护资金项目的名称
		QueryFilter queryFilter = new QueryFilter();
		//年度养护资金详情查询
		queryFilter.addFilter("attmentYear", startTime, MatchType.GE);
		queryFilter.addFilter("attmentYear", endTime, MatchType.LE);
		double total =0; //检查得分
		double percentage =0; //系统完善度
		//返回时间
		int years[] = new int[Integer.parseInt(endTime) - Integer.parseInt(startTime)+1];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		double tableFull = MathsUtils.mulDouble(yearPart, years.length);    //表格总分
		tableFull = MathsUtils.sumDouble(tableFull, yearFull);    
		//文件详情总览()
		dataMengementList = this.getDataMengementList(queryFilter);
		
		if(yhkxList.size() > 0){
			total = MathsUtils.sumDouble(total, yearFull);
		}
		for (DataMengement dataMengement : dataMengementList) {
			double temp = 0;      //年度得分
			if (dataMengement.getAttachments().size() > 0){
				for (Attachment attachment : dataMengement.getAttachments()) {
					if ("jcgf_yhjh".equals(attachment.getTag())){
						total = MathsUtils.sumDouble(total, yearPart);
						temp += total;
						break;
					}
				}
			} 
			dataMengement.setRemark(String.valueOf(temp));    //年度得分
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		tableFull = MathsUtils.formatDouble(tableFull,2);
		percentage = MathsUtils.divDouble(total *100 ,tableFull, 2) ;
		map.put("percent", percentage);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		//页面初始化时   属性为null  会导致空指针异常
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("yhkxList", yhkxList);
		map.put("att", list);
		map.put("years", years);
		map.put("overView", dataMengementList);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
